//
//  PublicMethod.m
//  CarService
//
//  Created by Nimo.Yu on 15/1/30.
//  Copyright (c) 2015年 lexingsoft. All rights reserved.
//

#import "PublicMethod.h"
#import <SVPullToRefresh/SVPullToRefresh.h>

#define IOS_VERSION_7_OR_ABOVE (([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)? (YES):(NO))

@implementation PublicMethod
///设置下拉刷新样式
+ (void)setPullToRefreshView:(UIScrollView *)scrollView 
{
    [scrollView.pullToRefreshView setTitle:NSLocalizedString(@"下拉刷新页面", nil) forState:
     SVInfiniteScrollingStateStopped];
    [scrollView.pullToRefreshView setTitle:NSLocalizedString(@"释放后即可更新", nil) forState:
     SVInfiniteScrollingStateTriggered];
    [scrollView.pullToRefreshView setTitle:NSLocalizedString(@"加载中...", nil) forState:
     SVInfiniteScrollingStateLoading];
    UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"01_00000"]];
    UIImageView *stopImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"01_00000"]];
//        imageView.frame=CGRectMake(0, 0, 50, 50);
    imageView.contentMode=UIViewContentModeCenter;
    //    imageView.backgroundColor=[UIColor groupTableViewBackgroundColor];
    NSMutableArray *imageArray=[NSMutableArray array];
    for (int i=0; i<25; i++) {
        NSString *fileName=[NSString stringWithFormat:@"01_000%02d",i];
        UIImage *image=[UIImage imageNamed:fileName];
        [imageArray addObject:image];
    }
    imageView.animationImages=imageArray;
    imageView.animationDuration=2;
    [imageView startAnimating];
    
//    if ([data isKindOfClass:[NSDictionary class]])
//    {
//        NSMutableArray *stopImageArray=[NSMutableArray array];
//        for (int i=0; i<19; i++)
//        {
//            NSString *fileName=[NSString stringWithFormat:@"02_000%02d",i];
//            UIImage *image=[UIImage imageNamed:fileName];
//            [stopImageArray addObject:image];
//        }
//        stopImageView.animationImages=stopImageArray;
//        stopImageView.animationDuration=2;
//        [stopImageView startAnimating];
//    }
    
    [scrollView.pullToRefreshView setCustomView:imageView forState:SVPullToRefreshStateLoading];
    [scrollView.pullToRefreshView setCustomView:stopImageView forState:SVPullToRefreshStateTriggered];
    [scrollView.pullToRefreshView setCustomView:stopImageView forState:SVPullToRefreshStateStopped];
}


+ (void)setFinishRefreshView:(UIScrollView*)scrollView
{
    UIImageView *stopImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"02_00000"]];
    
    NSMutableArray *stopImageArray=[NSMutableArray array];
    for (int i=0; i<19; i++)
    {
        NSString *fileName=[NSString stringWithFormat:@"02_000%02d",i];
        UIImage *image=[UIImage imageNamed:fileName];
        [stopImageArray addObject:image];
    }
    stopImageView.animationImages=stopImageArray;
    stopImageView.animationDuration=2.0;
    [stopImageView startAnimating];
    
    [scrollView.pullToRefreshView setCustomView:stopImageView forState:SVPullToRefreshStateLoading];
}


+ (void) setExtraCellLineHidden:(UITableView *) tableView{
    UIView *view =[[UIView alloc]init];
    view.backgroundColor=[UIColor clearColor];
    [tableView setTableFooterView:view];
//    [tableView setTableHeaderView:view];
}

+ (void) solveCellLineMigrationScheme:(UITableView *) tableView{
    //iOS7解决Cell分割线偏移方案
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    //iOS8解决Cell分割线偏移方案
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

+ (UIImage *) resizeImageWithImage:(UIImage *) flagImage SpecialSize:(BOOL) useSpecialSize{
    CGRect resizeRect;
    
    resizeRect.size = flagImage.size;
    CGSize maxSize = CGSizeMake(1136, 1136);
    if (useSpecialSize ) {
        maxSize = CGSizeMake(480, 480);
    }
    if (resizeRect.size.width > maxSize.width)
        resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);
    if (resizeRect.size.height > maxSize.height)
        resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);
    
    resizeRect.origin = (CGPoint){0.0f, 0.0f};
    UIGraphicsBeginImageContext(resizeRect.size);
    [flagImage drawInRect:resizeRect];
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resizedImage;
}

+ (void) startLocation
{
}

+ (CGSize)getStringSize:(NSString *)contentString FontSize:(UIFont *)font textSize:(CGSize)textSize
{
    CGRect wk_textRect;
    if(IOS_VERSION_7_OR_ABOVE)
    {
        NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                    font, NSFontAttributeName,
                                    nil];
        wk_textRect = [contentString boundingRectWithSize:textSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    }
    else
    {
        wk_textRect.size = [contentString sizeWithFont:font constrainedToSize:textSize lineBreakMode:NSLineBreakByClipping];
    }
    wk_textRect.size.height = wk_textRect.size.height;
    return wk_textRect.size;
}


+ (NSString *)getCachesPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documents = [paths objectAtIndex:0];
    return documents;
}

+ (NSString *)getUserSoundPath
{
    NSString *soundPath = [[PublicMethod getCachesPath] stringByAppendingPathComponent:@"chatVoice"];
    NSFileManager *wk_manager = [NSFileManager defaultManager];
    BOOL wk_isExist,wk_isDirectory;
    wk_isExist = [wk_manager fileExistsAtPath:soundPath isDirectory:&wk_isDirectory];
    if(wk_isExist == NO || wk_isDirectory == NO)
    {
        [wk_manager createDirectoryAtPath:soundPath withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    return soundPath;
}

+(BOOL)systemIsLaterThanString:(NSString *)version
{
    NSString *osVersion = [[UIDevice currentDevice] systemVersion];
    BOOL noRotationNeeded = ([version compare:osVersion options:NSNumericSearch]
                             != NSOrderedDescending);
    return noRotationNeeded;
}
@end
